package com.core.family.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.core.common.domain.response.R;
import com.core.common.utils.ExcelReadUtil;
import com.core.common.utils.ExcelUtil;
import com.core.family.admin.domain.PanData;
import com.core.family.admin.domain.req.PanDataReq;
import com.core.family.admin.domain.req.UserReq;
import com.core.family.admin.service.PanDataService;
import com.core.family.mybatisplus.MapperAndServiceUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author honglei
 * @version 1.0
 * @time: 2024-5-30 16:26:30
 * @Description 服务实现层 网盘资料表
 */
@Slf4j
@Api(tags = "PanDataController 网盘资料表")
@RestController
@RequestMapping("/api/PanData/")
public class PanDataController {


    @Resource
    PanDataService panDataService;


    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping(value = "getPage", method = {RequestMethod.GET, RequestMethod.POST})
    public R getPage(@RequestBody PanDataReq queryReq) {

        return R.ok(panDataService.selectPage(queryReq));
    }

    /**
     * 批量查询
     *
     * @return
     */
    @RequestMapping(value = "getList", method = {RequestMethod.GET, RequestMethod.POST})
    public R getList(@RequestBody UserReq queryReq) {
        return R.ok(panDataService.selectList(queryReq));
    }

    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping(value = "simpleU", method = {RequestMethod.GET, RequestMethod.POST})
    public R simpleU(String str, @RequestParam(required = false) String grp) {


        return R.ok(panDataService.simpleU(str, grp));
    }


    /**
     * 普通更新
     *
     * @param list
     * @return
     */
    @PostMapping(value = "updateBatch")
    public R updateBatch(@RequestBody List<PanData> list) {

        return R.ok(MapperAndServiceUtil.getService(PanData.class).updateBatchById(list));
    }


    /**
     * 新增
     *
     * @param list
     * @return
     */
    @PostMapping(value = "saveBatch")
    public R save(@RequestBody List<PanData> list) throws Exception {
        return R.ok(MapperAndServiceUtil.getService(PanData.class).saveBatch(list, 0));
    }


    /**
     * 删除
     *
     * @param request
     * @param id
     * @return
     */
    @PostMapping(value = "del/{id}")
    public R del(HttpServletRequest request, @PathVariable("id") Serializable id) {
        return R.ok(MapperAndServiceUtil.getService(PanData.class).removeById(id));
    }


    @PostMapping(value = "getExcelTp")
    public void importExcel(HttpServletResponse response) {
        ExcelUtil.downloadXLSX(response, "网盘资料", new String[]{"link", "mark"}, null);
    }

    @PostMapping(value = "importExcel")
    public R importExcel(HttpServletResponse response, MultipartFile file) {
        Map<String, List<Map<String, Object>>> mapList = ExcelReadUtil.parseAllFrom(file);
        log.info("importExcel.mapList:{}", mapList.size());

        panDataService.replaceBatch(panDataService.formatBean(mapList));


        return R.ok();
    }


}
